package trees;

public class BinarySearch {
    public int search(int[] nums, int target) {
        int left = 0;
        int right = nums.length;

        while (left <= right) {
            int middle = (left + right) / 2;
            if (middle >= nums.length) {
                return -1;
            }
            if (nums[middle] > target) {
                right = middle - 1;
            } else if (nums[middle] < target) {
                left = middle + 1;
            } else {
                return middle;
            }
        }

        return -1;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{-1, 0, 3, 5, 9, 12};
        int target = 12;
        BinarySearch search = new BinarySearch();
        System.out.println(search.search(nums, target));
        ;
    }
}
